package com.gxa.shop.servlet.carousel;

import com.alibaba.fastjson.JSON;
import com.gxa.shop.bean.dto.Page;
import com.gxa.shop.bean.pojo.Carousel;
import com.gxa.shop.commons.r.R;
import com.gxa.shop.service.carousel.CarouselService;
import com.gxa.shop.service.carousel.impl.CarouselServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * com.gxa.shop.servlet.carousel
 * User: 刁彦侨
 * Date: 2024/12/19 10:20
 * motto:   逆水行舟不进则退
 * Description:
 * Version: V1.0
 */
@WebServlet(name = "carousel.FindByPageServlet" ,value = "/carousel/findByPage")
public class FindByPageServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String str_page = request.getParameter("page");
        String str_limit = request.getParameter("limit");

        Page page = new Page();

        if (str_page.trim().length()>0){
            try {
                page.setPage(Integer.parseInt(str_page));
            }catch (Exception e){
                page.setPage(1);
            }
        }
        if (str_limit.trim().length()>0){
            try {
                page.setLimit(Integer.parseInt(str_limit));
            }catch (Exception e){
                page.setLimit(10);
            }
        }
        CarouselService carouselService = new CarouselServiceImpl();
        Long total = carouselService.findByTotal();
        //满足条件的总条数
        System.out.println(total);
        //当前页码的数据
        List<Carousel> list = carouselService.findByPage(page);
        R r= R.ok().addData("total",total).addData("list",list);
        PrintWriter writer = response.getWriter();
        String result = JSON.toJSONString(r);
        System.out.println("FindByPageServlet:"+result);
        writer.write(result);
        writer.flush();
        writer.close();
    }
}
